import urllib

import requests
import json
import datetime
import sys

url = 'http://127.0.0.1:10000/jypay/business_settled'
import requests
import time
import aes
import base64
import hashlib


def pay_core(url, data_obj):
    data = json.dumps(data_obj)
    cipher = aes.AesEncryption(aeskey)
    data = cipher.encrypt(data)
    data = base64.urlsafe_b64encode(data).decode('utf-8')
    request_obj = {
        'merchantid': merchantid,
        'data': data,
        'timestamp': str(int(time.time()))
    }
    sign_string = secretkey + merchantid + data + request_obj['timestamp'] + secretkey
    sign = hashlib.sha256(sign_string.encode("utf-8")).hexdigest()
    request_obj['sign'] = sign
    body = urllib.parse.urlencode(request_obj)
    print(body)
    response = requests.post(url, data=request_obj)
    rsp_data = json.loads(response.content.decode('utf-8'))
    print(response.content)
    if rsp_data['message'] != 'success':
        print(response.content)
        return
    rsp_sign_string = secretkey + rsp_data['merchantid'] + rsp_data['data'] + rsp_data['timestamp'] + secretkey
    rsp_sign = hashlib.sha256(rsp_sign_string.encode('utf-8')).hexdigest()
    if rsp_sign != rsp_data['sign']:
        print("sign error")
    data = base64.urlsafe_b64decode(rsp_data['data'].encode('utf-8'))
    result = cipher.decrypt(data)
    print(result)


order_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
if __name__ == '__main__':
    merchantid = "M2017051513600452200"
    secretkey = "1234567890123456"
    aeskey = "6543210987654321"
    if sys.argv[1] == "set":
        data_obj = {
            "username": "潘豪",
            "usertel": "13418071372",
            "usercardno": "6222023602098798689",
            "bankname": "工商银行",
            "userpid": "362201198602190014",
            "feerate": "3.5",
            "d0rate": "0.3",
            "settlefee": "0",
            "maxfee": "2800",
            "bankunion": "102100099996",
            "ordernumber": "test" + str(int(time.time())),
            "backurl": "http://127.0.0.1:9999/testnotify",
            "province": "广东省",
            "city": "广州市",
            "area": "天河区",
            "address": "广东省广州市天河区科韵路"
        }
        # url = 'http://127.0.0.1:9989/pay/lepos/merchant_reg'
        url = 'https://api.3035pay.com/pay/etcsd/merchant_reg'
    elif sys.argv[1] == "pay":
        merno = sys.argv[2]
        money = sys.argv[3]
        data_obj = {
            "sub_merchant_id": merno,
            "ordertime": order_time,
            "ordernumber": datetime.datetime.now().strftime("%Y%m%d%H%M%S"),
            "amount": money,

            "username": "田永城",
            "usertel": "15902093492",
            "cardno": "6225768761098720",
            "userpid": "44142419920620353X",
            # "bankunion": "102100099996",
            "ordernumber": "test" + str(int(time.time())),
            "backurl": "http://127.0.0.1:9999/testnotify",
            "latitude": "22.967626",
            "longitude": "114.014589",
            "returnurl": "http://123.207.247.101/noti/leshua",
            "feerate": "3.0",
            "body": "test",
            "cvn2": "868",
            "expdate": "2101"

        }
        url = 'http://127.0.0.1:9989/pay/lepos/merchant_pay'
    elif sys.argv[1] == 'etcsd':
        data_obj = {
            "ordernumber": datetime.datetime.now().strftime("%Y%m%d%H%M%S"),
            "sub_merchant_id": "M10003117120001",
            "amount": "1000000",
            "backurl": "http://127.0.0.1:9999/testnotify",
        }
        url = 'https://api.3035pay.com/pay/etcsd/merchant_pay'

    elif sys.argv[1] == 'status':
        merno = sys.argv[2]
        data_obj = {
            "MERCAHNTNO": merno,
            "RSPTIME": order_time,
            "ORDERNO": datetime.datetime.now().strftime("%Y%m%d%H%M%S"),
            "RMARK": "测试",
            "OPEMONDY": "1",
            "PAYTYPE": "KJ"
        }
        url = 'http://127.0.0.1:10012/jypay/mer_status'
        rsp = requests.post(url, json.dumps(data_obj))
        print(rsp.content)
        exit()

    print(json.dumps(data_obj))
    # c = input("confirm c contune ,e exit")
    # if c != 'c':
    #     exit()
    pay_core(url, data_obj)
